<!DOCTYPE html>
<title>Test to make sure removing a media element's source(s) does not cause a crash.</title>
<script src="../resources/testharness.js"></script>
<script src="../resources/testharnessreport.js"></script>
<script src="media-file.js"></script>
<script>
function test_remove_source(testFunction) {
    async_test(function(t) {
        var video = document.createElement("video");

        video.onloadstart = t.step_func(function() {
            testFunction(video);
            setTimeout(t.step_func_done(), 100);
        });

        // Add a bunch of source elements with bogus urls because we want to remove elements
        // after the media engine begins processing sources, and we can't predict the delay
        // between when the media element fires an "error" event and our handler is called,
        // but we need to guarantee that there are <source> elements that haven't been
        // processed when we run the test.
        for (var index = 1; index <= 10; index++)
            addSource(index);

        function addSource(index) {
            source = document.createElement("source");
            source.src = index + "-" + Date.now() + ".ogv";
            source.type = mimeTypeForExtension(source.src.split(".").pop());
            video.appendChild(source);
        }
    }, "source elements removed using " + testFunction.name + "()");
}

function removeChild(video) {
    // Removing all "source" elements with "removeChild()".
    var sources = video.childNodes;
    for (var source of sources)
        video.removeChild(source);
}

function innerHTML(video) {
    // Removing all "source" elements by setting "innerHTML".
    video.innerHTML = "";
}

function replaceChild(video) {
    // Removing all "source" elements with "replaceChild()".
    var sources = video.childNodes;
    var span = document.createElement("span");
    span.appendChild(document.createTextNode("Yo"));
    for (var source of sources)
       video.replaceChild(span, source);
}

test_remove_source(removeChild);
test_remove_source(innerHTML);
test_remove_source(replaceChild);
</script>